package edu.npu.cv.controller;

import edu.npu.cv.constant.TemplatePath;
import edu.npu.cv.entity.PublicationDO;
import edu.npu.cv.service.PublicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(TemplatePath.S + TemplatePath.PUBLICATION)
public class PublicationController {

    @Autowired
    private PublicationService publicationService;

    /**
     * 从数据库获取成员列表
     */
    private List<PublicationDO> getpublicationList(){
        return publicationService.findAll();
    }

    /**
     * 查询所有成员
     * @return
     */
    @GetMapping
    public ModelAndView list(Model model){
        List<PublicationDO> publicationList = getpublicationList();
        //1、map方法，将对象PublicationDO 的year属性组织成list
        //2、stream().distinct()：将list中的值去重
        List<String> yearList = publicationList.stream().map(PublicationDO::getYear).collect(Collectors.toList()).stream().collect(Collectors.toList());
        List<String> yearList1 = new ArrayList<>(new HashSet<String>(yearList)); //去重后的集合
        Collections.sort(yearList1);
        System.out.println(yearList1);
        model.addAttribute("yearList",yearList1);
        model.addAttribute("publicationList",publicationList);
        return new ModelAndView("publication","peopleModel",model);
    }

}
